var express = require("express");
var router = express.Router();
const Result = require("../utils/result");
const foodService = require("../services/foodService");

// 获得分类
router.get("/", async function (req, res) {
  const foodcate = await foodService.getFoodcate();
  res.json(Result.success({ foodcate }));
});

// 根据食物分类查询食物;
router.post("/foodcate", async function (req, res) {
  const { foodcate, userId } = req.body;
  const food = await foodService.getFood(foodcate,userId);
  res.json(Result.success({ food }));
});
// 获取食物详情
router.get("/detail", async function (req, res) {
  const { foodId,userId } = req.query;
  const detail = await foodService.getFoodDetail(foodId,userId);
  res.json(Result.success(detail));
});
// 搜索食物
router.get("/search", async function (req, res) {
  const { value } = req.query;
  const search = await foodService.search(value);
  res.json(Result.success(search));
});
// 获取常用和收藏
router.get("/oftenAndCollect", async function (req, res) {
  const userId = req.query.userId;
  const { oftenUseArr, collectArr } =
    await foodService.getOftenAndCollect(userId);
  res.json(Result.success({ oftenUseArr, collectArr }));
});

module.exports = router;
